import {
  applyDecorators,
  createParamDecorator,
  ExecutionContext,
  SetMetadata,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';

export const RequrieLoginKey = 'requrieLogin';
export const RequrieLogin = () => SetMetadata(RequrieLoginKey, true);

export const RequriePermissionKey = 'requriePermission';
export const RequriePermission = (...permission: string[]) => {
  return SetMetadata(RequriePermissionKey, permission);
};

export const GetUserInfo = createParamDecorator(
  (data: string, ctx: ExecutionContext) => {
    return data
      ? ctx.switchToHttp().getRequest().user[data]
      : ctx.switchToHttp().getRequest().user;
  },
);

export const uploadFilter = (name = 'file', op: MulterOptions = {}) => {
  return applyDecorators(UseInterceptors(FileInterceptor(name, op)));
};
